<?php
/**
 *
 * @author 天意
 * @version 1.0
 */

include_once 'dingwork/model/controller/interface/Action.php';
class Controller{
	private $app = null;
	private $_type = array();
	private $_Config = array();
	private $_Controller = null;
	private $_Action = null;
	private $_Router = array();
	private $_Param = array();
	private $_Actioninfo = array();
	public function __construct(){
		$this->app = Regsiter::getInterface();
		$this->_type['_Config'] = $this->app->get('Config');
		$this->_type['_Db'] = $this->app->get('Db');
		$this->_type['_View'] = $this->app->get('View');
		$this->_type['_Router'] = $this->app->get('Router');
		$this->_type['_Param'] = $this->app->get('Param');
		$this->_Router = $this->_type['_Router']->getRouter();
		$this->_Actioninfo = array(
					'Crotroller' => $this->_getController(),
					'Action' =>	$this->_getAction()
				);
		$this->_Controller();
	}

	private function _Controller(){
		$c = $this->_getController();
		$Controllerfile = app_path.'./controller/'.$c.'Action.php';
		try{
			if(file_exists($Controllerfile))
			{
				require_once $Controllerfile;
				$Controller = $c.'_Action';
				if(class_exists($Controller)){
					$this->_Controller = new $Controller($this->_type,$this->_Actioninfo);
					$this->_Action();
				}else{
					throw new ControllerException('没有找到控制器:'.$c);
				}
			}else{
				throw new ControllerException('没有找到控制器:'.$c);
			}
		}catch(ControllerException $e){
			$this->_errorCatch(1);
		}
	}
	private function _Action(){
		$Action = $this->_getAction();
		try{
			if(method_exists($this->_Controller,$Action) || method_exists($this->_Controller,'__call')){
				$this->_Action = call_user_func(array($this->_Controller,$Action));
			}else{
				throw new ActionException('控制器'.$this->_getController().'没有找到动作:'.$Action);
			}
		}catch(ActionException $e){
			$this->_errorCatch(2);
		}
	}
	private function _throwException($catch){
		if($catch == 1){
			throw new ControllerException('没有找到控制器:'.$this->_getController());
		}elseif($catch ==2){
			throw new ActionException('控制器'.$this->_getController().'没有找到动作:'.$this->_getAction());
		}
	}
	private function _errorCatch($catch){
		if(!empty($this->_type['_Config']['error_controller']) && !empty($this->_type['_Config']['error_action'])){
			$error_controller = ucfirst($this->_type['_Config']['error_controller']);
			$ErrorControllerfile = app_path.'./controller/'.$error_controller.'Action.php';
			if(file_exists($ErrorControllerfile)){
				require_once $ErrorControllerfile;
				$error_controller = $error_controller.'_Action';
				if(class_exists($error_controller)){
					$ErrorController = new $error_controller($this->_type,$this->_Actioninfo);
					call_user_func(array($ErrorController,$this->_type['_Config']['error_action']));
				}else{
					$this->_throwException($catch);
				}
			}else{
				$this->_throwException($catch);
			}
		}else{
			$this->_throwException($catch);
		}
	}
	private function _getController(){
		return ucfirst($this->_Router['c']);
	}
	private function _getAction(){
		return $this->_Router['a'];
	}
}

abstract class Action implements Action_interface{
	protected $_config ,$_db,$_view,$_router=null;
	protected $_Actioninfo = array();
	final public function __construct($app,$Actioninfo){
		$this->_config = $app['_Config'];
		$this->_db = $app['_Db'];
		$this->_view = $app['_View'];
		$this->_router = $app['_Router'];
		$this->_request = $app['_Param'];
		$this->_Actioninfo = $Actioninfo;
		$this->init();
	}
	public function init(){
		//检测初始化模块是否存在
		if(method_exists(parent,'init'))
			parent::init();
	}
	final public function getAction(){
		return $this->_Actioninfo['Action'];
	}
	final public function getController(){
		return $this->_Actioninfo['Crotroller'];
	}


}
class ControllerException extends DingException{

}
class ActionException extends DingException{
}
